#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };
using pii = pair<int, int>;
int m = 0, n = 0;
class Solution {
public:
    int bfs(vector<vector<int>>& grid, vector<vector<bool>>& sta, int i, int j)
    {
        queue<pii> q;
        q.push({ i,j });
        sta[i][j] = true;
        int count = 1;
        while (!q.empty())
        {
            int a = q.front().first;
            int b = q.front().second;
            q.pop();
            for (int k = 0;k < 4;k++)
            {
                int x = a + dx[k], y = b + dy[k];
                if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && sta[x][y] == false)
                {
                    q.push({ x,y });
                    sta[x][y] = true;
                    ++count;
                }
            }
        }
        return count;
    }
    int maxAreaOfIsland(vector<vector<int>>& grid)
    {
        m = grid.size();
        n = grid[0].size();
        vector<vector<bool>> sta(m, vector<bool>(n, false));
        int Max = 0;
        for (int i = 0;i < m;i++)
        {
            for (int j = 0;j < n;j++)
            {
                if (grid[i][j] == 1 && sta[i][j] == false)
                {
                    int ret = bfs(grid, sta, i, j);
                    Max = max(Max, ret);
                }
            }
        }
        return Max;
    }
};